Skip to content

refactor(api): migrate 38 routes to withAuth + 401 JSON for /api (Phase 1)#16

Merged
gurdenbatra merged 1 commit into
mainfrom
phase-1-withauth-bulk
Jun 12, 2026
Merged

refactor(api): migrate 38 routes to withAuth + 401 JSON for /api (Phase 1)#16
gurdenbatra merged 1 commit into
mainfrom
phase-1-withauth-bulk

Conversation

@gurdenbatra

Copy link
Copy Markdown
Member

What

Completes the withAuth rollout started in #14. 38 routes migrated from hand-rolled createClient()+getUser()→401 boilerplate (three stylistic variants) to the single withAuth wrapper. 40 routes total now use it.

Auth-only and mechanical — success/error response payloads preserved verbatim (no ok()/fail() conversion), as are maxDuration exports, streaming responses, business logic, and status codes. Done via 5 parallel agents on disjoint file sets, then verified centrally.

Middleware: unauthenticated /api/* now returns a 401 JSON envelope instead of a 307 redirect to /login — fetch clients can't follow that redirect and were getting an opaque HTML response. Page navigations still redirect; webhook routes and /api/auth stay bypassed.

Excluded (intentionally still hand-roll auth)

  • auth/callback — Supabase login plumbing; wrapping it would break login
  • integrations/slack/oauth + slack/callback — delicate OAuth redirect/cookie flows, low consistency value
  • Integration webhook routes (slack/events, notion/webhook, folk/sync) never used getUser (HMAC-verified)

Minor behavior notes (reviewed, judged safe)

  • signals 401 body drops a success:false field — 401 path only; clients treat 401 as auth-fail regardless.
  • convergence/snapshots now runs auth before its goal_space_id 400 check, so an unauthenticated request missing the param gets 401 instead of 400 (401 is the more correct precedence).
  • graph/edges GET and graph/nodes GET were already public reads with no auth check — left unwrapped (protected by middleware); wrapping would have added a new 401 path (a behavior change beyond an auth-only transform).

Test plan

  • clean tsc --noEmit → 0 errors
  • eslint . → 0 errors
  • vitest run → 560 pass (co-located route tests updated to pass a Request to wrapped handlers)
  • spot-checked streaming (reflection/run) and param (edges/[id], nested portfolios/[id]/steps/[step]) migrations by hand

Final Phase 1 PR alongside merged #13/#14/#15. Remaining Phase 1 items — generated Supabase types + migrations-dir reorg — are deferred pending the Supabase CLI.

Completes the withAuth rollout started in the foundation PR. All standard
user-authed routes now use withAuth instead of hand-rolling
createClient()+getUser()→401, eliminating three stylistic variants of the same
boilerplate. Auth-only, mechanical: success/error response payloads are
preserved verbatim (no ok()/fail() conversion), as are maxDuration exports,
streaming responses, business logic, and status codes.

Middleware: unauthenticated /api/* now gets a 401 JSON envelope instead of a
307 redirect to /login (fetch clients can't follow that). Page navigations
still redirect; webhook routes and /api/auth stay bypassed.

Excluded (still hand-roll auth, intentionally): auth/callback (Supabase login
plumbing), integrations/slack/oauth + slack/callback (delicate OAuth redirect
flows). Integration webhook routes never used getUser.

Minor behavior notes: signals 401 body drops a `success:false` field (401 path
only); convergence/snapshots runs auth before its 400 param check (401 now
precedes 400 for that edge case). graph GET reads were already public and stay
unwrapped (protected by middleware).

Updated co-located route tests to pass a Request to wrapped handlers.
Verified: tsc 0, lint 0, 560 tests pass.
@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cof-learning-system Ready Ready Preview, Comment Jun 12, 2026 10:14am

Request Review

@gurdenbatra gurdenbatra merged commit 13da515 into main Jun 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant